import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'

export const menuRoutes = [{
  path: '/system',
  component: Layout,
  redirect: '/system/user',
  name: 'System',
  roleId: 'A1',
  meta: {
    title: 'System',
    icon: 'el-icon-setting'
  },
  children: [{
    path: 'user',
    name: 'User',
    roleId: 'A1.1',
    component: () => import('@/views/system/user/index'),
    meta: {
      title: 'User',
      icon: 'user'
    }
  },
  {
    path: 'role',
    name: 'Role',
    roleId: 'A1.2',
    component: () => import('@/views/system/role/index'),
    meta: {
      title: 'Role',
      icon: 'el-icon-lock'
    }
  },
  {
    path: 'books/setting',
    name: 'BooksSetting',
    roleId: 'A1.3',
    meta: {
      title: 'BooksSetting',
      icon: 'el-icon-copy-document'
    },
    children: [{
      path: 'category',
      name: 'BooksCategory',
      roleId: 'A1.3.1',
      component: () => import('@/views/system/books/category/index'),
      meta: {
        title: 'BooksCategory',
        icon: 'el-icon-tickets'
      }
    },
    {
      path: 'type',
      name: 'BooksType',
      roleId: 'A1.3.2',
      component: () => import('@/views/system/books/type/index'),
      meta: {
        title: 'BooksType',
        icon: 'el-icon-tickets'
      }
    }
    ]
  }
  ]
},

{
  path: '/books',
  component: Layout,
  redirect: '/books/index',
  roleId: 'A2',
  name: 'Books',
  children: [{
    path: 'index',
    name: 'BooksIndex',
    roleId: 'A2.1',
    component: () => import('@/views/books/index'),
    meta: {
      title: 'Books',
      icon: 'el-icon-box'
    }
  }]
}
]

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Library',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Library', icon: 'dashboard' }
    }]
  },

  ...menuRoutes,

  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
